{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# file name meaning\n",
    "\n",
    "eg. 21_cairo_inception_2000_64_15_1.ipynb\n",
    "\n",
    "`[tag]_[dataset_name]_[method_name]_[data_length]_[batch_size]_[epochs]_[calculate_unit]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from tools import general\n",
    "import os\n",
    "from tools.configure.constants import DATASETS_CONSTANT, INCEPTION_CONSTANT, METHOD_PARAMETER_TEMPLATE, JUPYTER_TOKEN\n",
    "from HAR_main import train_val\n",
    "\n",
    "\n",
    "nb_name_file = general.ipy_nb_name(JUPYTER_TOKEN[\"token_lists\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "theNotebook = nb_name_file.split('_')\n",
    "assert len(theNotebook) == 6 or len(theNotebook) == 7, 'The number of parameters should be 6 / 7'\n",
    "\n",
    "global title_param\n",
    "title_param = {\n",
    "    'num': theNotebook[0],\n",
    "    'dataset': theNotebook[1],\n",
    "    'model_name': theNotebook[2],\n",
    "    'data_length': int(theNotebook[3]),\n",
    "    'batch_size': int(theNotebook[4]),\n",
    "    'epochs': int(theNotebook[5]),\n",
    "    'calculation_unit': theNotebook[6] if len(theNotebook)==7 else '0',  # If not specified, the CPU is used to train the model\n",
    "}\n",
    "\n",
    "# assert title_param['dataset'] in ['cairo', 'milan', 'kyoto7', 'kyoto8', 'kyoto11'], 'Please correct the dataset name！'\n",
    "# assert title_param['model_name'] in ['WCNN','LSTM', 'inception'], 'The method is wrong. Please correct it！'\n",
    "# assert title_param['data_length'] in [2000], 'The method is wrong. Please correct it！'\n",
    "# assert title_param['batch_size'] in [64], 'The method is wrong. Please correct it！'\n",
    "# assert title_param['epochs'] in [15, 30, 1500], 'The method is wrong. Please correct it！'\n",
    "# assert title_param['calculation_unit'] in ['0', '1', '2'], 'Error in calculation unit selection, please correct！'\n",
    "\n",
    "display(title_param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 数据参数\n",
    "\n",
    "# 模型公共参数\n",
    "nb_epochs = title_param['epochs']\n",
    "batch_size = title_param['batch_size']\n",
    "\n",
    "calculation_unit = title_param['calculation_unit']\n",
    "\n",
    "# 模型私有参数\n",
    "model_parameter_dict = INCEPTION_CONSTANT\n",
    "\n",
    "dict_config_cus = {\n",
    "    \"datasets_dir\": DATASETS_CONSTANT[\"base_dir\"],  # 这是公共数据集常量\n",
    "    \"archive_name\": DATASETS_CONSTANT[\"archive_name\"],\n",
    "    \"ksplit\": DATASETS_CONSTANT[\"ksplit\"],\n",
    "    \"dataset_name\": title_param['dataset'],\n",
    "    \"data_lenght\": title_param['data_length'],\n",
    "    \"distance_int\": None,\n",
    "\n",
    "    \"nb_epochs\": nb_epochs,\n",
    "    \"batch_size\": batch_size,\n",
    "\n",
    "    \"reTrain\": True,\n",
    "    \"calculation_unit\": calculation_unit,\n",
    "}\n",
    "\n",
    "general.Merge(dict_config_cus, model_parameter_dict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "\n",
    "if dict_config_cus['calculation_unit'] is '0':\n",
    "    os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"-1\"\n",
    "else:\n",
    "    if dict_config_cus['calculation_unit'] is '1':\n",
    "        # 使用第0块GPU\n",
    "        os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
    "\n",
    "    else:\n",
    "        os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"1\"\n",
    "\n",
    "    config = tf.ConfigProto()\n",
    "    config.gpu_options.allow_growth = True\n",
    "    session = tf.Session(config=config)\n",
    "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[origin](#origin)\n",
    "\n",
    "[C](#constrain)\n",
    "\n",
    "[CS_1](#constrain_1)\n",
    "\n",
    "[CS_2](#constrain_2)\n",
    "\n",
    "[CS_3](#constrain_3)\n",
    "\n",
    "[CS_4](#constrain_4)\n",
    "\n",
    "[CS_5](#constrain_5)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='origin'>origin</a>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '9999'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='constrain'>C</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '999'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='constrain_1'>CS_1</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '1'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='constrain_2'>CS_2</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '2'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='constrain_3'>CS_3</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '3'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='constrain_4'>CS_4</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '4'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <a id='constrain_5'>CS_5</a>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "dict_config_cus['distance_int'] = '5'\n",
    "train_val(dict_config_cus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "general.Merge(METHOD_PARAMETER_TEMPLATE, dict_config_cus)\n",
    "general.showResult(METHOD_PARAMETER_TEMPLATE)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "HARedit",
   "language": "python",
   "name": "haredit"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}